﻿using System.Collections.Generic;
using System.Web.Mvc;
using ControleRepresentante.Logic;
using ControleRepresentante.Logic.Models;
using ControleRepresentante.Logic.Repositories;
using ControleRepresentante.Logic.Repositories.SearchExtensions;
using WebSite.ViewModels.ViewModelExtensions.DetailsViewModelExtensions;
using WebSite.ViewModels.ViewModelExtensions.EditViewModelExtensions;
using WebSite.ViewModels.ViewModelExtensions.IndexViewModelExtensions;
using ControleRepresentante.Common;

namespace WebSite.Controllers.ControllerExtensions
{
    public class RelatorioVisitaControllerJolitexExtension : ControllerJolitexExtensionBase<RelatorioVisita>
    {
        protected override IIndexViewModelExtension DoGetIndexViewModelExtension(FormCollection formCollection, IRepository repository)
        {
            return new RelatorioVisitaJolitexIndexViewModel(GetOcorrencia(formCollection));
        }

        private static RelatorioVisitaJolitexOcorrencia? GetOcorrencia(FormCollection formCollection)
        {
            string ocorrenciaString = formCollection["ocorrencia"];
            return ocorrenciaString == null
                       ? (RelatorioVisitaJolitexOcorrencia?) null
                       : ocorrenciaString.ParseToEnum<RelatorioVisitaJolitexOcorrencia>();
        }

        public override IEditViewModelExtension EditViewModelExtension
        {
            get { return new RelatorioVisitaJolitexEditViewModel(); }
        }

        public override IDetailsViewModelExtension DetailsViewModelExtension
        {
            get { return new RelatorioVisitaJolitexDetailsViewModel(); }
        }

        public override ISearchExtension GetSearchExtension(FormCollection formCollection)
        {
            return new RelatorioVisitaSearchExtension(GetOcorrencia(formCollection));
        }

        public override void RegisterIndexColumns(IIndexColumnsProvider indexColumnsProvider)
        {
            indexColumnsProvider.RegisterColumn("Ocorrência", RelatorioVisitaJolitexIndexColums.Ocorrencia);
        }

        protected override Dictionary<string, string> TryUpdateModel(RelatorioVisita model, FormCollection formCollection)
        {
            model.RelatorioVisitaJolitex.Ocorrencia = GetOcorrencia(formCollection).Value;
            return new Dictionary<string, string>();
        }

        protected override Area Area { get { return Area.RelatorioVisita; } }

        protected override void SetModel(RelatorioVisita model)
        {
            if (model.RelatorioVisitaJolitex == null)
            {
                model.SetRelatorioVisitaJolitex();
            }
        }
    }
}